package so.wy.servicecenter.businessbed.manager.inner;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import so.wy.servicecenter.base.exception.ManagerException;
import so.wy.servicecenter.base.frame.manager.AbstractManager;
import so.wy.servicecenter.businessbed.dao.rdb.hospital.hospital.HospitalDO;
import so.wy.servicecenter.businessbed.dao.rdb.hospital.hospital_price.HospitalPriceDO;
import so.wy.servicecenter.businessbed.dao.rdb.hospital.hospital_rent_time.HospitalRentTimeDAO;
import so.wy.servicecenter.businessbed.dao.rdb.hospital.hospital_rent_time.HospitalRentTimeDO;
import so.wy.servicecenter.businessbed.dao.rdb.order.order.OrderDO;
import so.wy.servicecenter.businessbed.manager.fm.HospitalFManager;
import so.wy.servicecenter.data.dao.enums.YesNoEnum;

import java.util.Date;

@Service
@Scope("prototype")
public class HospitalIManager extends AbstractManager {
    @Autowired
    private HospitalFManager hospitalFManager;

    @Autowired
    private HospitalRentTimeDAO rentTimeDAO;

    //校验医院是否可以租赁
    public HospitalPriceDO checkHospitalCanOrder(Long hospitalId) throws ManagerException {
        HospitalDO hospitalInfo = hospitalFManager.getHospitalInfo(hospitalId);
        if (hospitalInfo == null) {
            throw exception("医院不存在!");
        }

        HospitalPriceDO priceInfo = hospitalFManager.getHospitalPriceInfo(hospitalId);
        if (priceInfo == null) {
            throw exception("医院未设置租赁价格信息!");
        }

        // 校验医院当前时间段是否可以租赁
        if (rentTimeDAO.selectCount(new LambdaQueryWrapper<HospitalRentTimeDO>()
                .eq(HospitalRentTimeDO::getHospitalId, hospitalId)
                .eq(HospitalRentTimeDO::getIsOpen, YesNoEnum.YES.getCode())) > 0) {
            if (rentTimeDAO.countNotRent(hospitalId) > 0) {
                throw exception("当前时间段，设备不可租赁");
            }
        }

        return priceInfo;
    }

    //医院对应订单完成
    public void orderFinish(OrderDO orderDO) {
        HospitalDO hospitalInfo = hospitalFManager.getHospitalInfo(orderDO.getHospitalId());

        hospitalInfo.setCountOrder(hospitalInfo.getCountOrder() + 1);
        hospitalInfo.setAmountTotal(hospitalInfo.getAmountTotal().add(orderDO.getOrderAmount()));

        hospitalFManager.updateHospitalInfo(hospitalInfo);
    }

    //医院对应订单完成
    public void orderPayFree(OrderDO orderDO) {
        HospitalDO hospitalInfo = hospitalFManager.getHospitalInfo(orderDO.getHospitalId());

        hospitalInfo.setAmountPayFree(hospitalInfo.getAmountPayFree().add(orderDO.getOrderAmount()));

        hospitalFManager.updateHospitalInfo(hospitalInfo);
    }
}
